﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;

namespace DAO
{
    public class connectDB
    {
        public static MySqlConnection conn;

        public static void open_DB()
        {
            try
            {

                conn = new MySqlConnection("Server=192.168.1.7;Database=qlsv1213 ;Uid=cdbd;Pwd=cdbd;Character Set=utf8");
                //conn = new SqlConnection("Data Source=192.168.1.35;Integrated Security = SSPI;initial catalog=QLPM; Connection Timeout=120");
                //conn = new SqlConnection("Data Source = localhost; Initial Catalog = QLPM; Integrated Security = SSPI;Connection Timeout=10");

                conn.Open();
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi: Không kết nối được với Database \n " + ex.ToString());

            }
        }
        public static void close_DB()
        {
            conn.Close();
        }


        public static DataSet BindDataAll(string StoreProName)
        {
            DataSet ds = new DataSet();
            try
            {
                open_DB();
                MySqlCommand cmd = new MySqlCommand();
                cmd.CommandText = StoreProName;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = conn;
                MySqlDataAdapter da = new MySqlDataAdapter();
                da.SelectCommand = cmd;
                da.Fill(ds);

            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                close_DB();
            }
            return ds;
        }


        public static DataTable BindTBDataAll(string StoreProName)
        {
            DataTable dt = new DataTable();
            try
            {
                open_DB();
                MySqlCommand cmd = new MySqlCommand();
                cmd.CommandText = StoreProName;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = conn;
                MySqlDataAdapter da = new MySqlDataAdapter();
                da.SelectCommand = cmd;
                da.Fill(dt);

            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                close_DB();
            }
            return dt;
        }

    }
}
